package com.hxy.travel.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.hxy.travel.constant.MessageConstant;
import com.hxy.travel.constant.RedisConstant;
import com.hxy.travel.entity.PageResult;
import com.hxy.travel.entity.QueryPageBean;
import com.hxy.travel.entity.Result;
import com.hxy.travel.pojo.OrderSetting;
import com.hxy.travel.pojo.Setmeal;
import com.hxy.travel.service.OrderSettingService;
import com.hxy.travel.service.SetmealService;
import com.hxy.travel.service.TravelGroupService;
import com.hxy.travel.utils.POIUtils;
import com.hxy.travel.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.*;

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

//     @Reference
//     private SetmealService setmealService;
//
//    @Reference
//    private TravelGroupService travelGroupService  ;
//
//     @Autowired
//     private JedisPool jedisPool;
     @Reference
     private OrderSettingService OrderSettingService;


    @PostMapping("upload.do")
    public Result upload(MultipartFile excelFile){
        try {
            List<String[]> lists = POIUtils.readExcel(excelFile);

            List<OrderSetting> orderSettings = new ArrayList<>();

            for (String[] str : lists) {
                OrderSetting orderSetting = new OrderSetting();
                orderSetting.setOrderDate(new Date(str[0]));
                orderSetting.setNumber(Integer.parseInt(str[1]));
                orderSettings.add(orderSetting);

            }
            OrderSettingService.addOrderSetting(orderSettings);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ok(MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
        return Result.ok(MessageConstant.IMPORT_ORDERSETTING_SUCCESS);

    }

    @GetMapping("/getOrderSettingByMonth.do")
    public Result getOrderSettingByMonth(String date){
        List<Map> list = null;
        try {
            list = OrderSettingService.getOrderSettingByMonth(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.data(list, MessageConstant.GET_ORDERSETTING_SUCCESS);
    }
    @PostMapping("/setOrderSettingByDate.do")
    public Result setOrderSettingByDate( @RequestBody OrderSetting orderSetting ){

        System.out.println(orderSetting.toString());
        OrderSettingService.eidtOrderSetting(orderSetting);
        return Result.ok(MessageConstant.ORDERSETTING_SUCCESS);
    }


//    @PostMapping("/add.do")
//    public Result add(@RequestBody Setmeal setmeal, Integer[] travelgroupIds){
//
//        setmealService.addSetmealAndIds(setmeal,travelgroupIds);
//
//        return Result.ok(MessageConstant.ADD_SETMEAL_SUCCESS);
//
//    }
//    @PostMapping("/findPage.do")
//    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
//
//        PageResult list = setmealService.findPage(queryPageBean);
//
//        return list;
//
//    }
//    @GetMapping("/delete.do")
//    public Result delete(Integer id){
//
//        try {
//            travelItemService.deleteTravelItemById(id);
//            return Result.ok(MessageConstant.DELETE_TRAVELITEM_SUCCESS);
//        }catch (Exception e){
//            e.printStackTrace();
//            return Result.ok(MessageConstant.DELETE_TRAVELITEM_FAIL);
//        }
//
//    }
//    @GetMapping("/selectById.do")
//    public Result selectById(Integer id){
//
//      Setmeal setmeal=setmealService.selectById(id);
//
//      return Result.data(setmeal);
//
//    }
//
//    @GetMapping("/selectTravelGroupBySetmealId.do")
//    public Result selectTravelGroupBySetmealId( Integer id ) {
//        List<Integer> list = travelGroupService.selectTravelGroupBySetmealId(id);
//        return Result.data(list);
//    }
//
//    @PostMapping("/edit.do")
//    public Result edit( @RequestBody Setmeal setmeal ,Integer[] travelgroupIds ) {
//
//        setmealService.editSetmeal(setmeal, travelgroupIds);
//
//        return Result.ok(MessageConstant.EDIT_TRAVELGROUP_SUCCESS);
//    }
//    @GetMapping("/delete.do")
//    public Result delete( Integer id ){
//        try {
//            travelGroupService.deleteTravelItemById(id);
//        } catch ( RuntimeException e ){
//            e.printStackTrace();
//            return Result.fail(e.getMessage());
//        } catch ( Exception e) {
//            e.printStackTrace();
//            return Result.ok(MessageConstant.DELETE_TRAVELGROUP_FAIL);
//        }
//        return Result.fail(MessageConstant.DELETE_TRAVELGROUP_SUCCESS);
//    }
//    @GetMapping("/findAll.do") public Result findAll(){
//        List<TravelGroup> list = travelGroupService.findAll();
//        return Result.data(list,MessageConstant.QUERY_TRAVELGROUP_SUCCESS);
//    }


//    @PostMapping("/edit.do")
//    public Result edit(@RequestBody TravelItem travelItem){
//
//        travelItemService.editTravelItem(travelItem);
//
//
//        return Result.ok(MessageConstant.EDIT_TRAVELITEM_SUCCESS);
//
//    }
//

}
